package com.ydtg.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.ydtg.bean.Account;
import com.ydtg.dao.AccountDao;
import com.ydtg.dao.BaseDao;

public class AccountDaoImpl extends BaseDao implements AccountDao{
	public List<Account> getall(Account news) {
		// TODO 自动生成的方法存根
		Connection conn = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		List<Account> list = new ArrayList<Account>();
		
		try {
			conn = getconnection();
			String sql = "select id,user,pwd,name,staff from account where 1=1";
			stmt = conn.prepareStatement(sql);
			rs = stmt.executeQuery();
			while (rs.next()) {
				Account pet  = new Account();
				pet.setId(rs.getInt(1));
				pet.setUser(rs.getString(2));
				pet.setName(rs.getString(4));
				pet.setStaff(rs.getInt(5));
				list.add(pet);
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally {
			this.closeAll(conn, stmt, rs);
		}
		
		return list;
	}

	@Override
	public int add(Account pet) {
		
		String psql = "insert into account(user,pwd,name,staff) "
				+ "values(?,?,?,?)";
		Object[] params = {pet.getUser(),pet.getPwd(),pet.getName(),pet.getStaff()};
		
		int count=this.executeUpdate(psql, params);
		return count;
	}

	@Override
	public int updateByID(Account pet) {
		
		String psql = "	UPDATE account set user=?,pwd=?,name=?,staff=? where id=?";
		Object[] params = {pet.getUser(),pet.getPwd(),pet.getStaff(),pet.getId()};
		int count =  this.executeUpdate(psql, params);
		return count;
	}

	@Override
	public int delByID(int id) {
		String psql = "	delete from account where id=?";
		Object[] params = {id};
		int count = this.executeUpdate(psql, params);
		return count;
	}

	@Override
	public List<Account> getTypefaceList(Account pet) {
		Connection conn = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		List<Account> list = new ArrayList<Account>();
		try {
			conn = getconnection();
			String psql = "select id,user,pwd,name,staff from account where 1=1";
			if (pet!=null&&pet.getId()!=0) {
				psql = psql+" and id="+pet.getId();
			}
			if (pet!=null&&pet.getUser()!=null&&!"".equals(pet.getUser())) {
				psql = psql+" and user='"+pet.getUser()+"'";
			}
			if (pet!=null&&pet.getPwd()!=null&&!"".equals(pet.getPwd())) {
				psql = psql+" and pwd='"+pet.getPwd()+"'";
			}
			if (pet!=null&&pet.getName()!=null&&!"".equals(pet.getName())) {
				psql = psql+" and name='"+pet.getName()+"'";
			}
			if (pet!=null&&(pet.getStaff()==0||pet.getStaff()==1)) {
				psql = psql+" and staff="+pet.getStaff();
			}
			stmt = conn.prepareStatement(psql);
			rs = stmt.executeQuery();
			while (rs.next()) {
				Account pet1  = new Account();
				pet1.setId(rs.getInt(1));
				pet1.setUser(rs.getString(2));
				pet1.setPwd(rs.getString(3));
				pet1.setName(rs.getString(4));
				pet1.setStaff(rs.getInt(5));
				list.add(pet1);
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally {
			closeAll(conn, stmt, null);
		}
		return list;
	}
}
